package com.ulic.customerManApplication.code;

import java.util.HashMap;
import java.util.Map;


public class CodeTypeUtil {
	private static Map<CodeType, CodeVO[]> codeMap = new HashMap<CodeType, CodeVO[]>();
	
	public static void setCode(CodeType type, CodeVO[] vos){
		codeMap.put(type, vos);
	}
	
	public static String getName(CodeType type, int value){
		CodeVO[] vos = codeMap.get(type);
		for(int i = 0; i < vos.length; i++){
			CodeVO vo = vos[i];
			if(vo.getValue() == value){
				return vo.getName();
			}
		}
		return "&nbsp;";
	}
	
	public static String getOptionHTML(CodeType type, int value){
		CodeVO[] vos = codeMap.get(type);
		String html = "";
		for(int i = 0; i < vos.length; i++){
			CodeVO vo = vos[i];
			html = html + "<option value=\"" + vo.getValue() + "\"";
			if(value == vo.getValue()){
				html = html + " selected";
			}
			html = html + ">" + vo.getName() + "</option>";
		}
		return html;
	}
	
	public static String getRadioHTML(CodeType type, String name, int value){
		CodeVO[] vos = codeMap.get(type);
		String idPre = String.valueOf(Math.random());
		String html = "";
		for(int i = 0; i < vos.length; i++){
			CodeVO vo = vos[i];
			html = html + "<input type='radio' value=\"" + vo.getValue() + "\" name=\"" + name + "\" id=\"" + idPre + vo.getValue() + "\"";
			if(value == vo.getValue()){
				html = html + " checked=\"checked\"";
			}
			html = html + "><label for=\"" + idPre + vo.getValue() + "\">" + vo.getName() + "</label>";
		}
		return html;
	}
	
	public static CodeVO[] getCode(CodeType type){
		return codeMap.get(type);
	}
}
